package com.yq.news.controller;

import com.yq.news.api.NewsEditApi;
import com.yq.news.pojo.News;
import com.yq.news.pojo.Result;
import com.yq.news.service.EditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;

@RestController
public class NewsEditController implements NewsEditApi {
    @Autowired
    private EditService editService;
    @Override
    public Result addNews(News news) {
        Result result = new Result();
        try {
            news.setCreatedate(new Date());
            this.editService.addNews(news);
            result.setSuccess(true);
            result.setMessage("添加列表数据成功");
            return result;
        }
        catch (Exception e)
        {
            System.out.println(e);
            result.setSuccess(false);
            result.setMessage("添加列表数据失败");
        }
        return result;
    }

    @Override
    public News findNewsById(int id) {
        return this.editService.findNewsById(id);
    }

    @Override
    public Result updateNews(News news) {
        Result result = new Result();
        try {
            news.setCreatedate(new Date());
            this.editService.updateNews(news);
            result.setSuccess(true);
            result.setMessage("修改成功");
            return result;
        }catch (Exception e){
            System.out.println(e);
            result.setMessage("修改失败");
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public Result delectNewsByIds(int[] ids) {
        Result result = new Result();
        try {
            this.editService.delectNewsByIds(ids);
            result.setSuccess(true);
            result.setMessage("删除成功");
            return result;
        }catch (Exception e){
            System.out.println(e);
            result.setMessage("删除失败");
            result.setSuccess(false);
        }
        return result;
    }
}
